package com.bbh.plugin.single.privilege.demo.controller;

import com.bbh.plugin.single.privilege.core.entity.Admin;
import com.bbh.plugin.single.privilege.core.enums.AdminEnum;
import com.bbh.plugin.single.privilege.core.service.AdminService;
import com.bbh.plugin.single.privilege.core.service.ResourceService;
import com.bbh.plugin.single.privilege.core.service.RoleService;
import com.bbh.common.domain.Message;
import com.bbh.common.domain.StatusType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.EnumSet;

/**
 * Created by roman_000 on 2015/7/15.
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BasicController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private ResourceService resourceService;

    /**
     * 入口页面
     *
     * @return
     */
    @RequestMapping(VIEW_FILED + "/index")
    public String index(Model model) {

        // model.addAttribute("enabledItems", EnumSet.allOf(AdminEnum.IS_ENABLED.class));
        model.addAttribute("lockedItems", EnumSet.allOf(AdminEnum.IS_LOCKED.class));
        // model.addAttribute(CRUMBS_ATTRIBUTE, buildCrumbsMap(null,"系统管理","用户管理"));
        return "single/admin/index";
    }

    /**
     * 跳转到edit页面
     *
     * @return
     */
    @RequestMapping(VIEW_FILED + "/edit")
    public String edit(String id, Model model) {
        if (null != id) {
            Admin admin = adminService.selectByPrimaryKey(id);
            model.addAttribute("admin", admin);
        }
        return "single/admin/edit";
    }


    /*@RequestMapping("/list")
    public
    @ResponseBody
    DataTableVo<Admin> list(HttpServletRequest request) throws ServletRequestBindingException {
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        Page pageRequest = initPageRequest(request);
        Page<Admin> page = adminService.selectPage(searchParams, null, pageRequest);
        DataTableVo<Admin> dataTableVo = new DataTableVo(request, page);
        return dataTableVo;
    }*/

    @RequestMapping(value = VIEW_FILED + "/insert")
    public
    @ResponseBody
    Message insert(@Valid Admin admin) {

        Message message = new Message(StatusType.SUCCESS.getKey(), "保存成功");
        return message;
    }

    @RequestMapping(VIEW_FILED + "/delete")
    public
    @ResponseBody
    Message delete(@RequestParam(value = "ids[]") String[] ids) {
        adminService.delete(ids);
        Message message = new Message(StatusType.SUCCESS.getKey(), "删除成功");
        return message;
    }

    @RequestMapping(VIEW_FILED + "/update")
    public
    @ResponseBody
    Message update(@Valid Admin admin) {
        Assert.notNull(admin.getId(), "参数错误");
       /* Admin editAdmin = adminService.selectByPrimaryKey(admin.getId());
        if (editAdmin != null) {
            editAdmin.setIsEnabled(null == admin.getIsEnabled() ? false : admin.getIsEnabled());
            editAdmin.setName(admin.getName());
            editAdmin.setEmail(admin.getEmail());
            editAdmin.setOrganizationId(admin.getOrganizationId());
            editAdmin.setIsLocked(null == admin.getIsLocked() ? false : admin.getIsLocked());
            editAdmin.setModifyDate(new Date());
            adminService.updateByPrimaryKeySelective(editAdmin);
        }
*/
        Message message = new Message(StatusType.SUCCESS.getKey(), "修改成功");
        return message;
    }

    @RequestMapping(VIEW_FILED + "/password/init")
    public
    @ResponseBody
    Message passwordInit(@Valid Admin admin) {
        Assert.notNull(admin.getId(), "参数错误");
        /*Admin editAdmin = adminService.select(admin.getId());
        if (editAdmin != null) {
            editAdmin.setModifyDate(new Date());
            adminService.entryptPassword(editAdmin);
            adminService.updateByPrimaryKeySelective(editAdmin);
        }*/
        Message message = new Message(StatusType.SUCCESS.getKey(), "密码初始化成功");
        return message;
    }

    /**
     * 用户关联角色界面
     *
     * @return
     */
    @RequestMapping(VIEW_FILED + "/authorization/edit")
    public String authorizationEdit(HttpServletRequest request, Model model) throws ServletRequestBindingException {
        return "single/admin/authorization";
    }

    /**
     * 用户关联角色界面
     *
     * @return
     */
   /*@RequestMapping(VIEW_FILED+"/authorization/list")
    public
    @ResponseBody
    DataTableVo<Role> authorizationList(Long adminId, String roleName, HttpServletRequest request) throws ServletRequestBindingException {
        Integer pageNumber = ServletRequestUtils.getIntParameter(request, PAGE_NUMBER);
        Integer pageSize = ServletRequestUtils.getIntParameter(request, PAGE_SIZE);
        Page pageRequest = new Page(null == pageNumber ? 0 : pageNumber, null == pageSize ? 5 : pageSize);
        Map<String, Object> params = Maps.newHashMap();
        params.put("adminId", adminId);
        params.put("roleName", roleName);
        Page<RoleVo> page = roleService.findAdminRolePage(params, ImmutableList.of(new Order("createDate", Order.DESC)), pageRequest);
        DataTableVo<Role> dataTableVo = new DataTableVo(request, page);
        return dataTableVo;
    }*/

    /**
     * 用户关联角色界面
     *
     * @return
     */
    /*@RequestMapping(VIEW_FILED+"/authorization/insert")
    public
    @ResponseBody
   Message authorizationInsert(String ids, Long adminId, HttpServletRequest request) throws ServletRequestBindingException {
        Assert.notNull(adminId, "参数错误");
        Map<String, String> params = Maps.newHashMap();
        if (StringUtils.isNotBlank(ids)) {
            JsonMapper mapper = JsonMapper.nonEmptyMapper();
            String result = StringEscapeUtils.unescapeHtml4(ids);
            params = mapper.fromJson(result, HashMap.class);
        }
        adminRoleService.insert(params, adminId);
        Message message = new Message(StatusType.SUCCESS.getKey(), "保存成功");
        return message;
    }*/

}
